package com.company.cloud.uba.modules.app.service.impl;

import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.AnonyTokenModel;
import com.company.cloud.common.redis.service.RedisUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.uba.modules.app.models.UserIdMappingUpdate;
import com.company.cloud.uba.modules.app.service.UserIdMappingService;
import com.company.cloud.uba.modules.base.entity.UserBrowseLog;
import com.company.cloud.uba.modules.base.entity.UserBrowseObjLog;
import com.company.cloud.uba.modules.base.entity.UserIdMapInfo;
import com.company.cloud.uba.modules.base.entity.UserShareInfo;
import com.company.cloud.uba.modules.base.service.UserBrowseLogService;
import com.company.cloud.uba.modules.base.service.UserBrowseObjLogService;
import com.company.cloud.uba.modules.base.service.UserIdMapInfoService;
import com.company.cloud.uba.modules.base.service.UserShareInfoService;
import com.company.cloud.uba.modules.cache.models.UserIdMappingModel;
import com.company.cloud.uba.modules.cache.service.UserIdMappingCacheService;
import com.company.cloud.uba.modules.cache.service.UserShareCacheService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserIdMappingServiceImpl implements UserIdMappingService {
    @Autowired
    private UserIdMapInfoService userIdMapInfoService;
    @Autowired
    private UserIdMappingCacheService userIdMappingCacheService;
    @Autowired
    private UserShareInfoService userShareInfoService;
    @Autowired
    private UserBrowseLogService userBrowseLogService;
    @Autowired
    private UserBrowseObjLogService userBrowseObjLogService;
    @Autowired
    private UserShareCacheService userShareCacheService;


    @Override
    public void addUserIdMapping(AnonyTokenModel anonyTokenModel) {
        UserIdMappingModel userIdMapping= userIdMappingCacheService.getUserIdMappingByCache(anonyTokenModel.getAnonyToken());
        if(userIdMapping!=null){
            return;
        }
        UserIdMapInfo entity=new UserIdMapInfo();
        entity.setId(anonyTokenModel.getAnonyToken());
        entity.setAnonyToken(anonyTokenModel.getAnonyToken());
        entity.setCman(-1);
        entity.setCdt(LocalDateTime.now());
        entity.setCip(anonyTokenModel.getIp());
        userIdMapInfoService.save(entity);
        userIdMappingCacheService.reloadUserIdMappingByCache(entity);
    }





    @Override
    public void updateUserIdMapping(UserIdMappingUpdate params) {
        ValidatorUtils.validate(params);
        UserIdMapInfo entity= userIdMapInfoService.getById(params.getAnonyToken());
        if(entity==null){
            return ;
        }
        if(CheckUtils.hasInt(entity.getUserId())){
            return ;
        }
        entity.setLoginToken(params.getLoginToken());
        entity.setCompanyNo(params.getCompanyNo());
        entity.setUserId(params.getUserId());
        entity.setClientType(params.getClientType());
        entity.setMobileNo(params.getMobileNo());
        entity.setEman(-1);
        entity.setEdt(params.getOpDate());
        entity.setEip(params.getOpIp());
        userIdMapInfoService.updateById(entity);
        userIdMappingCacheService.reloadUserIdMappingByCache(params.getAnonyToken());
    }

    private void update(UserIdMapInfo entity){
        //用户分享
        List<UserShareInfo> userShareList = userShareInfoService.list(new QueryWrapper<UserShareInfo>()
                .eq("anony_token",entity.getAnonyToken())
                .isNull("user_id")
        );
        if(!CollectionUtils.isEmpty(userShareList)){
            for(UserShareInfo item:userShareList){
                item.setCompanyNo(entity.getCompanyNo());
                item.setUserId(entity.getUserId());
                item.setToken(entity.getLoginToken());
                item.setEip(entity.getEip());
                item.setEdt(LocalDateTime.now());
                item.setEman(entity.getEman());
            }
            userShareInfoService.updateBatchById(userShareList);
            userShareCacheService.reloadUserShareByCache(userShareList);
        }
        //用户行为
        List<UserBrowseLog> userBrowseLogList= userBrowseLogService.list(new QueryWrapper<UserBrowseLog>()
                .eq("anony_token",entity.getAnonyToken())
                .isNull("user_id")
        );
        if(!CollectionUtils.isEmpty(userBrowseLogList)){
            for(UserBrowseLog userBrowseLog:userBrowseLogList){
                userBrowseLog.setCompanyNo(entity.getCompanyNo());
                userBrowseLog.setUserId(entity.getUserId());
                userBrowseLog.setUserToken(entity.getLoginToken());
                userBrowseLog.setEdt(LocalDateTime.now());
                userBrowseLog.setEip(entity.getEip());
                userBrowseLog.setEman(entity.getEman());
            }
            userBrowseLogService.updateBatchById(userBrowseLogList);
        }

        //用户行为对象
        List<UserBrowseObjLog> userBrowseObjLogList= userBrowseObjLogService.list(new QueryWrapper<UserBrowseObjLog>()
                .eq("anony_token",entity.getAnonyToken())
                .isNull("user_id")
        );
        if(!CollectionUtils.isEmpty(userBrowseObjLogList)){
            for(UserBrowseObjLog userBrowseObjLog:userBrowseObjLogList){
                userBrowseObjLog.setCompanyNo(entity.getCompanyNo());
                userBrowseObjLog.setUserId(entity.getUserId());
                userBrowseObjLog.setEdt(LocalDateTime.now());
                userBrowseObjLog.setEip(entity.getEip());
                userBrowseObjLog.setEman(entity.getEman());
            }
            userBrowseObjLogService.updateBatchById(userBrowseObjLogList);
        }
    }


}
